package org.ifilm.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.ifilm.model.UserJobPosting;
import org.ifilm.model.User;
import org.springframework.stereotype.Repository;

@Repository
public class NoticeDaoImpl implements NoticeDao {
	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public UserJobPosting createJobPosting(UserJobPosting jobPosting) {
		return entityManager.merge(jobPosting);
	}

	@Override
	public List<UserJobPosting> listJobPostings(User user) {
		try {
			Query query = entityManager.createQuery(listAllJobPostingsQuery());
			
			query.setParameter("poster", user);
			return (List<UserJobPosting>) query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private String listAllJobPostingsQuery() {
		StringBuilder strBuild = new StringBuilder();
		strBuild.append("select j from jobposting j where j.poster=:poster");
		return strBuild.toString();
	}
}
